//
//  MappingExampleViewController.m
//  MappingExample
//

#import "MappingExampleViewController.h"

@interface customAnnotation : NSObject <MKAnnotation>
{
	CLLocationCoordinate2D coordinate;
	NSString *title;
	NSString *subtitle;
}

@property (nonatomic, readonly) CLLocationCoordinate2D coordinate;
@property (nonatomic, retain) NSString *title;
@property (nonatomic, retain) NSString *subtitle;

- (id)initWithCoordinate:(CLLocationCoordinate2D)coords;

@end

@implementation customAnnotation

@synthesize coordinate, title, subtitle;

- (id)initWithCoordinate:(CLLocationCoordinate2D)coords {
	if (self = [super init]) {
		coordinate = coords;
	}
	return self;
}

- (void)dealloc {
	[title release];
	[subtitle release];
	
	[super dealloc];
}

@end


@implementation MappingExampleViewController

- (void)createAnnotationWithCoords:(CLLocationCoordinate2D)coords {
	customAnnotation *annotation = [[customAnnotation alloc] initWithCoordinate:coords];
	annotation.title = @"The Title";
	annotation.subtitle = @"Subtitle";
	[map addAnnotation:annotation];
	[annotation release];
}

- (void)viewDidLoad {
    [super viewDidLoad];
	
	map = [[MKMapView alloc] initWithFrame:[self.view bounds]];
	map.mapType = MKMapTypeSatellite;
	
	CLLocationCoordinate2D coords = CLLocationCoordinate2DMake(37.331689,-122.03071);
	
	float zoomLevel = 0.002;
	MKCoordinateRegion region = MKCoordinateRegionMake(coords,
													   MKCoordinateSpanMake(zoomLevel, zoomLevel));
	
	[map setRegion:[map regionThatFits:region] animated:YES];
	
	[self.view addSubview:map];
	
	map.delegate = self;
	[self createAnnotationWithCoords:coords];
}

- (MKAnnotationView *)mapView:(MKMapView *)mapView
			viewForAnnotation:(id <MKAnnotation>)annotation {

	MKAnnotationView *pinView;
	pinView = (MKAnnotationView *)[map dequeueReusableAnnotationViewWithIdentifier:annotation.title];

	if (pinView == nil)
		pinView = [[[MKAnnotationView alloc]
					initWithAnnotation:annotation reuseIdentifier:annotation.title] autorelease];
	else
		pinView.annotation = annotation;
	
	[pinView setImage:[UIImage imageNamed:@"iPhone.png"]];
	pinView.canShowCallout = TRUE;

	return pinView;
}

- (void)dealloc {
	[map release];
    [super dealloc];
}

@end
